package luke4fun.wheresmymoney.adapter.list.impl;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import luke4fun.wheresmymoney.R;
import luke4fun.wheresmymoney.adapter.list.BasicAdapter;
import luke4fun.wheresmymoney.bean.BudgetDetailBean;

public class BudgetDetailListAdapter extends BasicAdapter<BudgetDetailBean> {

	private List<BudgetDetailBean> dataList = new ArrayList<BudgetDetailBean>(0);
	private Context context;
	private LayoutInflater inflater;
	private int max;
	private boolean isOverBudget = false;
	private Map<String, BudgetDetailVH> viewMap = new HashMap<String, BudgetDetailVH>();
	
	public BudgetDetailListAdapter(Context context) {
		this.context = context;
		this.inflater = LayoutInflater.from(context);
	}
	
	@Override
	public int getCount() {
		return dataList.size();
	}

	@Override
	public Object getItem(int position) {
		return dataList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		convertView = inflater.inflate(R.layout.list_edit_budget_detail, null);
		
		BudgetDetailBean bean = dataList.get(position);
		final BudgetDetailVH holder = new BudgetDetailVH();
		holder.category = (TextView) convertView.findViewById(R.id.category);
		holder.budget	= (EditText) convertView.findViewById(R.id.budget_exp);
		holder.seekBar  = (SeekBar)  convertView.findViewById(R.id.seekBar);
		
		holder.category.setText(bean.getCategory());
		holder.category.setBackgroundColor(bean.getCategoryColor());
		holder.budget.setText(bean.getBudget() + "");
		holder.seekBar.setMax(bean.getMax());
		holder.seekBar.setProgress((int) bean.getBudget());
		
		if (isOverBudget) {
			holder.budget.setTextColor(context.getResources().getColor(android.R.color.holo_red_light));
		}
		
		holder.budget.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				Log.d("ZJ", "position " + position + " changed. new value= " + s.toString());
				int maxBudget = max;
				int sum = 0;
				for (int i=0; i<dataList.size(); i++) {
					if (i != position) {
						sum += dataList.get(i).getBudget();
					} else {
						if (!TextUtils.isEmpty(s.toString())) {
							int value = Integer.parseInt(s.toString());
							sum += value;
							dataList.get(i).setBudget(value);
							holder.seekBar.setProgress(value);
						}
					}
					Log.d("ZJ", "data at " + i + ": " + dataList.get(i).getBudget());
				}
				maxBudget -= sum;
				
				isOverBudget = (maxBudget < 0);
				if (isOverBudget) {
					for(BudgetDetailVH vh : viewMap.values()) {
						vh.budget.setTextColor(context.getResources().getColor(android.R.color.holo_red_light));
					}
				} else {
					for(BudgetDetailVH vh : viewMap.values()) {
						vh.budget.setTextColor(context.getResources().getColor(android.R.color.darker_gray));
					}
				}
			}
		});
		
		holder.seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				if (fromUser) {
					holder.budget.setText(String.valueOf(progress));
				}
			}
		});
		
		viewMap.put(bean.getCategory(), holder);
		return convertView;
	}

	@Override
	public void updateList(List<BudgetDetailBean> list) {
		dataList.clear();
		dataList.addAll(list);
	}
	
	public void updateMax(int max) {
		this.max = max;
		for (BudgetDetailBean dtl : dataList) {
			dtl.setMax(max);
		}
		notifyDataSetChanged();
	}

	static class BudgetDetailVH {
		public TextView category;
		public EditText budget;
		public SeekBar seekBar;
	}
}
